package com.i_art_engineer.meigong.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.i_art_engineer.meigong.AppConsts;
import com.i_art_engineer.meigong.AppContext;
import com.i_art_engineer.meigong.AppHttpClient;
import com.i_art_engineer.meigong.R;
import com.i_art_engineer.meigong.base.FullScreenActivity;
import com.i_art_engineer.meigong.base.ImmersiveActivity;
import com.i_art_engineer.meigong.photo.cache.Utils;
import com.i_art_engineer.meigong.util.DeviceUtils;
import com.i_art_engineer.meigong.util.ImageUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * 闪屏界面
 */
public class SplashActivity extends FullScreenActivity {

    private static final int GO_HOME = 1000;
    private static final int GO_GUIDE = 1001;
    private static final int GO_LOGIN = 1002;

    private static final long WELCOME_DELAY_MILLIS = 1000;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME:
                    goHome();
                    break;
                case GO_GUIDE:
                    goGuide();
                    break;
                case GO_LOGIN:
                    goLogin();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    public ImageView miv;
    public Bitmap bm=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        miv=(ImageView)findViewById(R.id.splash);
        bm= ImageUtils.readBitMap(this,R.drawable.splash);
        miv.setImageBitmap(bm);
        init();
    }

    private void init() {
        SharedPreferences preferences = AppContext.getPersistPreferences();
        boolean isFirstIn = preferences.getBoolean("isFirstIn_" + DeviceUtils.getVersionName(), true);

        if (isFirstIn) {
            preferences.edit().putBoolean("isFirstIn_" + DeviceUtils.getVersionName(), false).commit();
            handler.sendEmptyMessageDelayed(GO_GUIDE, WELCOME_DELAY_MILLIS);
        } else {
            checkToken();
        }
    }

    private void checkToken() {
        String token = AppContext.getTicket();
        if (TextUtils.isEmpty(token)) {
            handler.sendEmptyMessageDelayed(GO_LOGIN, WELCOME_DELAY_MILLIS);
        } else {
            final AsyncHttpResponseHandler jhandler = new JsonHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
        //            Log.i("校验结果", response.toString());
                    try {
                        int code = response.getInt("code");
                        if (code == 200) {
                            handler.sendEmptyMessageDelayed(GO_HOME, WELCOME_DELAY_MILLIS);
                        } else {
                            AppContext.cleanTicket();
                            handler.sendEmptyMessageDelayed(GO_LOGIN, WELCOME_DELAY_MILLIS);
                        }
                    } catch (JSONException e) {
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                    super.onFailure(statusCode, headers, throwable, errorResponse);
                    Toast.makeText(SplashActivity.this,throwable.getMessage()+"",Toast.LENGTH_SHORT).show();
                    AppContext.cleanTicket();
                    handler.sendEmptyMessageDelayed(GO_LOGIN, WELCOME_DELAY_MILLIS);
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    AppContext.cleanTicket();
                    handler.sendEmptyMessageDelayed(GO_LOGIN, WELCOME_DELAY_MILLIS);

                }
            };
            AppHttpClient.setTicket(token);
            AppHttpClient.post(AppConsts.API_ACCOUNT_AUTH, jhandler);
        }
    }

    private void goHome() {
        startActivity(new Intent(SplashActivity.this, MainActivity.class));
        finish();
    }

    private void goGuide() {
        startActivity(new Intent(SplashActivity.this, GuideActivity.class));
        finish();
    }

    private void goLogin() {
        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(bm!=null&&!bm.isRecycled())
        {
            bm.recycle();
            bm=null;
            miv.setImageBitmap(null);
        }
    }

}

